﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Shapes;

namespace InsuranceAdjuster.Controls
{
    public partial class PanelViewer : UserControl
    {
        public static readonly DependencyProperty SelectedPanelsProperty = DependencyProperty.Register(
            "SelectedPanels"
            , typeof(String)
            , typeof(FrameworkElement)
            , new PropertyMetadata(null, new PropertyChangedCallback(SelectedPanelsChanged)));

        public String SelectedPanels
        {
            get { return (String)GetValue(SelectedPanelsProperty); }
            set { SetValue(SelectedPanelsProperty, value); }
        }

        public PanelViewer()
        {
            InitializeComponent();
        }

        private static void SelectedPanelsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var ThisControl = sender as PanelViewer;

            UpdateShapes(ThisControl.SelectedPanels, ThisControl.LayoutRoot);
        }

        private static void UpdateShapes(String selectedPanels, DependencyObject element)
        {
            if (element is PanelShape)
            {
                var ThisPanelShape = element as PanelShape;
                if (IsSelected(ThisPanelShape.PanelName, selectedPanels))
                    ThisPanelShape.IsHighlighted = true;
                else
                    ThisPanelShape.IsHighlighted = false;
            }
            var ChildCount = VisualTreeHelper.GetChildrenCount(element);
            for(int x = 0; x < ChildCount; x++)
            {
                var ThisChild = VisualTreeHelper.GetChild(element, x);
                UpdateShapes(selectedPanels, ThisChild);
            }
        }
        private static bool IsSelected(String panelName, String selectedPanels)
        {
            if (String.IsNullOrEmpty(selectedPanels))
                selectedPanels = String.Empty;

            var SelectedPanels = selectedPanels.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var Panel in SelectedPanels)
            {
                if (Panel.Trim().Equals(panelName, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }
            return false;
        }

        private void PanelShape_Click(object sender, EventArgs e)
        {
            var ThisShape = sender as PanelShape;

            if (IsSelected(ThisShape.PanelName, this.SelectedPanels))
                RemovePanelFromSelection(ThisShape.PanelName);
            else
                AddPanelToSelection(ThisShape.PanelName);

            UpdateShapes(this.SelectedPanels, this.LayoutRoot);
        }
        private void AddPanelToSelection(string panelName)
        {
            if (String.IsNullOrEmpty(SelectedPanels))
            {
                SelectedPanels = panelName.Trim();
                return;
            }

            SelectedPanels += "; " + panelName.Trim();
            
        }
        private void RemovePanelFromSelection(string panelName)
        {
            if (String.IsNullOrEmpty(this.SelectedPanels))
                return;

            var NewSelectedPanels = String.Empty;

            var OldSelectedPanels = SelectedPanels.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var Panel in OldSelectedPanels)
            {
                if (Panel.Trim().Equals(panelName, StringComparison.InvariantCultureIgnoreCase))
                    continue;

                if (String.IsNullOrEmpty(NewSelectedPanels))
                    NewSelectedPanels = Panel.Trim();
                else
                    NewSelectedPanels += "; " + Panel.Trim();
            }
            this.SelectedPanels = NewSelectedPanels;
        }
    }

    public partial class PanelShape : UserControl
    {
        public static readonly DependencyProperty PanelNameProperty = DependencyProperty.Register("PanelName", typeof(String), typeof(PanelShape), null);
        public static readonly DependencyProperty NormalProperty = DependencyProperty.Register("Normal", typeof(Polygon), typeof(PanelShape), null);
        public static readonly DependencyProperty HighlightProperty = DependencyProperty.Register("Highlight", typeof(Polygon), typeof(PanelShape), null);
        public static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.Register("IsHighlighted", typeof(bool), typeof(PanelShape), null);

        public String PanelName
        {
            get { return (String)GetValue(PanelNameProperty); }
            set { SetValue(PanelNameProperty, value); }
        }
        public Polygon Normal
        {
            get { return (Polygon)GetValue(NormalProperty); }
            set
            {
                if (value == Normal)
                    return;

                SetValue(NormalProperty, value);
                if (IsHighlighted == false)
                {
                    this.Content = value;
                }
            }
        }
        public Polygon Highlight
        {
            get { return (Polygon)GetValue(HighlightProperty); }
            set
            {
                if (value == Highlight)
                    return;

                SetValue(HighlightProperty, value);
                if (IsHighlighted == true)
                {
                    this.Content = value;
                }
            }
        }
        public bool IsHighlighted
        {
            get { return (bool)GetValue(IsHighlightedProperty); }
            set
            {
                if (value == IsHighlighted)
                    return;

                SetValue(IsHighlightedProperty, value);

                if (IsHighlighted)
                    this.Content = Highlight;
                else
                    this.Content = Normal;
            }
        }

        public event EventHandler Click;

        public PanelShape()
        {
            //base.InitializeComponent();
            this.MouseLeftButtonUp += MouseUp;
        }

        private void MouseDown(object sender, MouseButtonEventArgs e)
        {
        }
        private void MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Click != null)
                Click.Invoke(this, EventArgs.Empty);
        }
    }
}

